package com.springcloud.coursedata.controller;


import com.entity.Course;
import com.entity.ResponseResultSet;
import com.springcloud.coursedata.service.impl.CourseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author
 * @create 2020-11-07 11:54
 */
//开启跨域访问的权限
//@CrossOrigin
@RefreshScope
@RequestMapping("/course")
@RestController
public class CourseController {

    @Value("${server.port}")
    String port;

    @Value("${configtest}")
    String configtest;

    @Autowired
    CourseServiceImpl courseService;

    @RequestMapping("/{cid}")
    public ResponseResultSet<Course> getOneCourse(@PathVariable String cid){
        ResponseResultSet<Course> resultSet = new ResponseResultSet(200,"success"+port,courseService.getCourseByCid(Integer.parseInt(cid)));
        return resultSet;
    }

    @RequestMapping("/courses")
    public ResponseResultSet<List<Course>> getCourses(){
        ResponseResultSet<List<Course>> resultSet = new ResponseResultSet<>(200,"success----"+port,courseService.getCourses());
        return resultSet;
    }
    //超时测试
    @RequestMapping("/timeout")
    public ResponseResultSet timeOut(){
        try {
            //让线程休眠1秒达到测试效果
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ResponseResultSet resultSet = new ResponseResultSet<>(500,"success----"+port,"timeOut Test");
        return resultSet;
    }

    //熔断器下高并发的测试
    @PostMapping("/ok")
    public ResponseResultSet Ok(){
        ResponseResultSet resultSet = new ResponseResultSet(200,"success","这个响应的速度很快");
        return resultSet;
    }

    @PostMapping("/addcourse")
    public String addCourse(@RequestBody Course course){
        courseService.addCourse(course);
        System.out.println(course);
        return "ok";
    }

    @RequestMapping("/configtest")
    public ResponseResultSet configTest(){
        return new ResponseResultSet(200,"Test ok",configtest);
    }

    @PostMapping("/testcourse")
    public String testCourse(@RequestBody String course){
        System.out.println("course:"+course);
        return "ok";
    }

    @PostMapping("/{pageIdex}/{pageSize}")
    public String Limit(@RequestBody String course,
                       @PathVariable("pageIndex") Integer pageIndex,
                       @PathVariable("pageSize") Integer pageSize){
        System.out.println(course);
        System.out.println(pageIndex);
        System.out.println(pageSize);
        return "limit ok ";
    }
}
